package com.wudl.flink.hbase.utils;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.BufferedMutator;
import org.apache.hadoop.hbase.client.BufferedMutatorParams;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;

import java.io.IOException;

/**
 * @author ：wudl
 * @date ：Created in 2021-12-08 23:57
 * @description：Hbase工具类
 * @modified By：
 * @version: 1.0
 */

public class HbaseUtils {
    private Connection conn = null;
    private static String tableName = "wudluser";

   private static   Connection instance = null;




    public static Connection getConnect() throws IOException {
        Connection conn = null;
        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        conf.set(HConstants.ZOOKEEPER_QUORUM,"192.168.1.161");
        conf.set(HConstants.ZOOKEEPER_CLIENT_PORT,"2181");
        conf.set(TableInputFormat.INPUT_TABLE,tableName);
        org.apache.hadoop.conf.Configuration hbaseConf = HBaseConfiguration.create(conf);
        //通过连接工厂创建连接
        conn = ConnectionFactory.createConnection(hbaseConf);

        return conn;
    }

    public static void  close(BufferedMutator mutator,Connection conn) throws IOException {
        if (mutator != null) {
            mutator.close();
        }
        if (!conn.isClosed()) {
            conn.close();
        }
    }
}
